package cn.lijigou.shopping;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;



import com.mapbar.android.maps.GeoPoint;
import com.mapbar.android.maps.ItemizedOverlay;
import com.mapbar.android.maps.MapActivity;
import com.mapbar.android.maps.MapController;
import com.mapbar.android.maps.MapView;
import com.mapbar.android.maps.OverlayItem;
import com.mapbar.android.maps.Projection;

public class MapRoutePlanActivity extends MapActivity {

	MapView mMapView;
	MapController mMapController;
	GeoPoint point;
	TipItemizedOverlay mTipItemizedOverlay;
	Drawable mTipBackground;

	/**
	 * 弹出的气泡View
	 */
	View popView;
	private double start_lat;
	private double start_lng;
	private double end_lat;
	private double end_lng;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_route_pla);
		
		Intent mIntent = this.getIntent();
		start_lat = mIntent.getDoubleExtra("start_lat", 0);
		start_lng = mIntent.getDoubleExtra("start_lng", 0);
		end_lat = mIntent.getDoubleExtra("end_lat", 0);
		end_lng = mIntent.getDoubleExtra("end_lng", 0);
		
		mMapView = (MapView) findViewById(R.id.itemizedoverlayview);
		mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
		mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放

		point = new GeoPoint((int) (end_lat * 1E6), (int) (end_lng * 1E6)); // 用给定的经纬度构造一个GeoPoint，单位是微度
																				// (度
																				// *
		// 1E6)
		mMapController.setCenter(point); // 设置地图中心点
		mMapController.setZoom(20);
		Drawable marker = getResources().getDrawable(R.drawable.map_icon); // 得到需要标在地图上的资源
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); // 为maker定义位置和边界
		// 接收前面传的参数
		Intent intent = getIntent();
		GeoPoint p1 = new GeoPoint((int) (end_lat * 1E6), (int) (end_lng * 1E6));
		List<OverlayItem> geoList = new ArrayList<OverlayItem>();
		geoList.add(new OverlayItem(p1, "", "辉煌国际"));
		// 添加IntentOverlay实例到mMapView
		mMapView.getOverlays().add(new OverItemT(marker, geoList, this));

		mTipBackground = getResources().getDrawable(R.drawable.tip_pointer_button_top);
		mTipItemizedOverlay = new TipItemizedOverlay(this, mTipBackground);
		mMapView.getOverlays().add(mTipItemizedOverlay);

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	class OverItemT extends ItemizedOverlay<OverlayItem> {
		private Drawable marker;
		List<OverlayItem> GeoList;

		public OverItemT(Drawable marker, List<OverlayItem> geoList, Context context) {
			super(boundCenterBottom(marker));
			this.marker = marker;
			this.GeoList = geoList;
			populate(); // createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {

			// Projection接口用于屏幕像素点坐标系统和地球表面经纬度点坐标系统之间的变换
			Projection projection = mapView.getProjection();
			for (int index = size() - 1; index >= 0; index--) { // 遍历GeoList
				OverlayItem overLayItem = getItem(index); // 得到给定索引的item
				String title = overLayItem.getTitle();
				// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
				Point point = projection.toPixels(overLayItem.getPoint(), null);
				Paint paintText = new Paint();
				paintText.setColor(Color.BLACK);
				paintText.setTextSize(15);
				// 绘制文本
				canvas.drawText(title, point.x, point.y - 25, paintText);
			}
			super.draw(canvas, mapView, shadow);
			// 调整一个durable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			// TODO Auto-generated method stub
			return GeoList.get(i);
		}

		@Override
		public int size() {
			// TODO Auto-generated method stub
			return GeoList.size();
		}

		@Override
		// 处理当点击事件
		protected boolean onTap(int i) {
			setFocus(GeoList.get(i));
			mTipItemizedOverlay.addOverlay(GeoList.get(i));
			return true;
		}
	}

	public void animateTo(GeoPoint pt) {
		mMapController.animateTo(pt);
	}
}
